<?php
	$user = $_SESSION [ 'user' ] ;
	
	if ( $user -> getType () == 1 )
	{
	
		if ( isset ( $_POST [ 'addcourse' ] ) ) {
				if ( empty ( $_POST [ 'name' ] ) || empty ( $_POST [ 'price'] ) ) {
					echo '<br /><h3><center>Alanlar Boş Bırakılamaz.</center></h3>' ;
					header ( "Refresh: 2; url= index.php?cont=addcourse" ) ;
				}else {
					$name = $_POST [ 'name' ] ;
					$db = new Database ( "localhost", "root", "deniztosur", "educationportal") ;
					$db -> connect () ;
					$db -> query ( "SELECT * FROM courses WHERE name = '$name' ");
					if ( $db -> getNumRows () != 0 ){
						echo '<br /><h3><center>Aynı Isimli Ders Adi Mevcut</center></h3>' ;
						header ( "Refresh: 2; url= index.php?cont=addcourse" ) ;
					} else {
						if ( isset ( $_SESSION [ 'user' ] ) ) {
							$user = $_SESSION [ 'user' ] ;
							$user_id = $user -> getUserId ();
							$price = $_POST [ 'price' ] ;
							$db -> query ( "INSERT INTO courses ( name , teacher_id , price ) VALUES ( '$name' , '$user_id' , '$price' ) ");
							$db -> query ( "SELECT course_id FROM courses WHERE name = '$name' ");
							if ( $db -> getNumRows () == 1 )
							{
								$result_object = mysql_fetch_object ( $db-> getQueryResult () ) ;
								$course_id = $result_object -> course_id;
								$db -> query ( "INSERT INTO student_courses ( student_id , course_id ) VALUES ( '$user_id' ,'$course_id' )" );
							} else {
								echo '<br /> Ders Ekleme Başarısız ' ;
							}
							echo '<br /><center>Ders Eklendi</center> <br/> ' ;
							header ( "Refresh: 2; url= index.php?cont=teaching" ) ;
						} else {
							echo '<br /> Yetkiniz Bulunmamaktadir... !!! ' ;
						}
					}
					
				}
				
			}else {
	
		echo ' 
		
		<br>
		<form name="addcourse" method="POST" action="#">
				<center><h1>Ders Ekleme Paneline Hoşgeldiniz</h1></center>
				<table align="center">
				<tr>
					<td>Ders Adı : </td>
					<td><input name="name" type="text"></td>			
				</tr>	
				<tr>
					<td>Ucreti : </td>
					<td><input name="price" type="text"></td>			
				</tr>	
				<tr>
					<td></td>
					<td align="center">
						<input type="submit" name="addcourse" value="Ekle">
						<input type="reset" name="clear" value="Temizle">	
					</td>					
				</tr>
			</table>			
		</form> ';
	
	}
	}else {
		header ( "Refresh: 2; url= index.php?" ) ;
	}
?>

